<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 style="text-align: center; margin-bottom: 30px;">系统登录</h2>

      <el-form :model="loginForm" @submit.prevent="handleLogin">
        <el-form-item>
          <el-input
              v-model="loginForm.yhm"
              placeholder="用户名"
              prefix-icon="User"
              size="large"
          />
        </el-form-item>

        <el-form-item>
          <el-input
              v-model="loginForm.pwd"
              placeholder="密码"
              prefix-icon="Lock"
              type="password"
              show-password
              size="large"
          />
        </el-form-item>

        <el-form-item style="margin-top: 30px;">
          <el-button
              type="primary"
              native-type="submit"
              style="width: 100%; height: 50px; font-size: 16px;"
          >
            登录
          </el-button>
        </el-form-item>
        <el-form-item style="margin-top: 30px;">
          <el-button
              type="info"
              style="width: 100%; height: 50px; font-size: 16px;"
              @click="router.push('/yycsh/sign')"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import axios from "axios";

const userStore = useUserStore()
const router = useRouter()

const loginForm = ref({
  yhm: '',
  pwd: ''
})

const handleLogin = async () => {
  if (!loginForm.value.yhm || !loginForm.value.pwd) {
    ElMessage.error('请输入用户名和密码')
    return
  }
  const {data} = await axios.get("/api/doLogin", {params:loginForm.value});
  if(data!=null && data!==''){
    userStore.setUsername(loginForm.value.yhm);
    userStore.setUserId(data.id);
    ElMessage.success('登录成功');
  }
  else{
    ElMessage.error('有户名或密码错误');
  }
  await router.push('/')
}
</script>

<style scoped>
.login-container {
  display: flex;
  position: fixed;
  right: 20px;
  align-items: center;
  min-height: 100vh;
  width: 500px;
}

.login-card {
  width: 400px;
  min-height: 350px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.el-input {
  height: 50px;
  font-size: 16px;
}
</style>